/**
 * 
 */
package xiaojian.toolkit.bean.tlv.test;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import xiaojian.toolkit.bean.tlv.decode.decoders.BeanTLVDecoder;
import xiaojian.toolkit.bean.tlv.encode.encoders.BeanTLVEncoder;
import xiaojian.toolkit.transport.DefaultEndpointFactory;
import xiaojian.toolkit.util.ByteUtils;


/**
 * @author hp
 *
 */
public class TlvDemo2 {

    public static void main(String[] args) {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
            	"xiaojian/toolkit/bean/tlv/test/tlvDemo2.xml");
    	
    	BeanTLVEncoder beanEncoder = (BeanTLVEncoder)ctx.getBean("beanEncoder");
    	
    	SampleTLV sample = new SampleTLV();
    	sample.setAge(101);
    	sample.setName("hello, world!");
    	
    	byte[]	bytes = ByteUtils.union( beanEncoder.encode(sample, 
    			beanEncoder.getEncodeContextFactory().createEncodeContext(SampleTLV.class, null)) );
    	
    	System.out.println( "TLV bytes is :\n" + ByteUtils.bytesAsHexString(bytes, 1024) );
    	
    	BeanTLVDecoder beanDecoder = (BeanTLVDecoder)ctx.getBean("beanDecoder");
    	Object bean = beanDecoder.decode(bytes.length, bytes, 
    			beanDecoder.getDecodeContextFactory().createDecodeContext(SampleTLV.class, null));
    	System.out.println( bean );
    	
//    	DefaultEndpointFactory sender = (DefaultEndpointFactory)ctx.getBean("outboundEndpointFactory");
//    	
//    	while (true) {
//    		sender.send(bean);
//    		try {
//				Thread.sleep(1000);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//    	}
    }
}
